home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Graphics⁄Sound / 3D graphics ƒ / openMandel.c < prev    next >
Text File  |  1990-01-04  |  3KB  |  158 lines

  1. /*
  2.     Copyright '89    Christopher Moll
  3.     all rights reserved
  4. */
  5.  
  6.  
  7. #include    "graph3D.h"
  8. #include    <stdio.h>
  9.  
  10. extern    int        numX, numY;
  11.  
  12. extern    Real    startX, startY;
  13. extern    Real    endX, endY;
  14. extern    Real    deltaX, deltaY;
  15. extern    int        numX, numY;
  16.  
  17. extern    Vector        maxVect, minVect;
  18.  
  19. extern    Real        *funcResults;
  20. extern    Boolean        vectrsCurrent;
  21.  
  22. OpenMandel()
  23. {
  24. static    Point    where = {100, 100};
  25.     long        theList[4];
  26.     SFReply        theReply;
  27.     FILE        *rdFile;
  28.     Boolean        StoreMandelPts(), GetMandHeader();
  29.     int            maxDwell;
  30.  
  31.     theList[0] = 'ManD';
  32.     
  33.     SFGetFile(where, "", NIL, 1, theList, NIL, &theReply);
  34.     if (theReply.good)
  35.     {
  36.         SetVol(NIL, theReply.vRefNum);
  37.         rdFile = fopen(ptoc((char *)theReply.fName), "r");
  38.         if (rdFile)
  39.         {
  40.             if (NOT(GetMandHeader(rdFile, &maxDwell)))
  41.                 goto Fail;
  42.             if (NOT(StoreMandelPts(rdFile, maxDwell)))
  43.                 goto Fail;
  44.  
  45. Fail:        fclose(rdFile);
  46.             vectrsCurrent = FALSE;
  47.             InvalidGraph();
  48.         }
  49.     }
  50. }
  51.  
  52. static
  53. Boolean
  54. GetMandHeader(rdFile, maxDwellP)
  55. FILE    *rdFile;
  56. int        *maxDwellP;
  57. {
  58.     FileSection    fileHead;
  59.     CoordBlock    cblock;
  60.     int            rdReslt;
  61.     char        dump[78];
  62.     Boolean        AllocatePts();
  63.  
  64.     rdReslt = fread(&fileHead, sizeof(FileSection), 1, rdFile);
  65.     rdReslt = fread(&cblock, sizeof(CoordBlock), 1, rdFile);
  66.     rdReslt = fread(&dump, 78, 1, rdFile);
  67.  
  68.     numX = cblock.height;
  69.     numY = cblock.width;
  70.     deltaX = cblock.maxDwell / 10.0;
  71.     deltaY = cblock.maxDwell / 10.0;
  72.     if (NOT(AllocatePts()))
  73.     {
  74.         MemAlert();
  75.         return(FALSE);
  76.     }
  77.     *maxDwellP = cblock.maxDwell;
  78.     return(TRUE);
  79. }
  80.  
  81. static
  82. Boolean
  83. StoreMandelPts(rdFile, maxDwell)
  84. FILE    *rdFile;
  85. int        maxDwell;
  86. {
  87.     register    int    xCnt, yCnt, dwellVal;
  88.     Real        currX, currY;
  89.     int            maxFunc, minFunc;
  90.     Real        xAtMax, yAtMax;
  91.     Real        xAtMin, yAtMin;
  92.     int            rdReslt, offSet;
  93.     int            *dwells;
  94.     Boolean        CmndPeriod();
  95.  
  96.     dwells = (int    *)NewPtr((long)numX * (long)numY * sizeof(int));
  97.     if (MemErr)
  98.         return(FALSE);
  99.     rdReslt = fread(dwells, numX * sizeof(int), numY, rdFile);
  100.     if (rdReslt NEQ numY)
  101.     {
  102.         DisposPtr(dwells);
  103.         return(FALSE);
  104.     }
  105.  
  106.     maxFunc = dwells[0];
  107.     minFunc = dwells[0];
  108.     xAtMax = startX;
  109.     yAtMax = startY;
  110.     xAtMin = startX;
  111.     yAtMin = startY;
  112.  
  113.     currX = startX;
  114. {FILE    *wrt;
  115. wrt=fopen("wrt","w");
  116.  
  117.     for (xCnt = 0; xCnt < numX; xCnt++)
  118.     {
  119.         currY = startY;
  120.         for (yCnt = 0; yCnt < numY; yCnt++)
  121.         {
  122.             offSet = (long)xCnt * numY + (long)yCnt;
  123.             dwellVal = dwells[offSet];
  124.             if (dwellVal > maxDwell)
  125.                 dwellVal = maxDwell;
  126. fprintf(wrt, "%d\t", dwellVal);
  127.             funcResults[offSet] = dwellVal;
  128.             if (dwellVal > maxFunc)
  129.             {
  130.                 maxFunc = dwellVal;
  131.                 xAtMax = currX;
  132.                 yAtMax = currY;
  133.             }
  134.             else if (dwellVal < minFunc)
  135.             {
  136.                 minFunc = dwellVal;
  137.                 xAtMin = currX;
  138.                 yAtMin = currY;
  139.             }
  140.             currY += deltaY;
  141.         }
  142. fprintf(wrt, "\n");
  143.         if (CmndPeriod())
  144.             break;  /* for */
  145.         currX += deltaX;
  146.     }
  147. fclose(wrt);
  148. }
  149.     maxVect.x = xAtMax;
  150.     maxVect.y = yAtMax;
  151.     maxVect.z = maxFunc;
  152.     minVect.x = xAtMin;
  153.     minVect.y = yAtMin;
  154.     minVect.z = minFunc;
  155.     DisposPtr(dwells);
  156.     return(TRUE);
  157. }
  158.